package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;
//实现
@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired  //在当前服务模块容器中找实现对象    从同一个服务中
    //@Reference//通过rpc协议从其他服务中寻找实现   从不同服务中寻找
    private UserInfoMapper userInfoMapper;
   @Reference
   private IUserInfoRedisService userInfoRedisService;

   @Override
    public UserInfo get(Long id) {

        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count>0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //1:创建短信验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);

        //2:创建一个短信,并发送短信
        StringBuilder builder = new StringBuilder(10);
        builder.append("你的注册短信验证码是:").append(code).append("请,你在")
                .append(Consts.VERIFY_CODE_VAI_TIME).append("分钟使用");
        //假装已经发送了短信
        System.out.println(builder);

        //3:将验证码缓存到redis里面
        userInfoRedisService.setVerifyCode(phone,code);
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException{
       //用户注册
       //1:判断参数是否为空
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"短信验证不能为空");
       //2:判断密码是否一致
        AssertUtil.isEquals(password,rpassword,"俩次密码不一致");
       //3:判断手机号码格式是否正确

       //4:判断手机号码是否唯一
        //通过上面已经有的方法来判断手机号是否唯一,自己调用自己的方法this
        if(this.checkPhone(phone)){
            throw new LogicException("手机号已经被注册");
        }
       //5:短信验证码是否一致
       String  code= userInfoRedisService.getVerifyCode(phone);
        //equalsIgnoreCase 判断两字符串忽略大小写是否相等
       if (code ==null || !code.equalsIgnoreCase(verifyCode)){
           throw new RuntimeException("验证码失效或者错误");
       }

        //6:用户注册
        UserInfo user = new UserInfo();
        user.setHeadImgUrl("/static/images/default.jpg");
        user.setLevel(1);
        user.setNickname(nickname);
        user.setPassword(password);
        user.setPhone(phone);
        user.setState(UserInfo.STATE_NORMAL);//状态控制
        userInfoMapper.insert(user);
   }

    @Override
    public String userLogin(String username, String password) throws LogicException {
        //分布式登录操作步骤:
        //1:获取参数username跟password,并作为条件,通过sql查询用户对象
        UserInfo userInfo = userInfoMapper.selectUserByUsernameAndPassword(username,password);
        //2:根据查询到的user对象判断是否存在,如果不存在,提示登录失败,如果存在,执行下一步操作
        if (userInfo==null){
            throw new LogicException("账号或者密码错误");
        }
        //3:登录成功之后,定义一个变量名为token,值为UUID创建随机数
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //4:将随机生成的token的值作为redis中的key,token中为value值,缓存到redis数据库中,同时设置token的有有效时间
        userInfoRedisService.setToken(token,userInfo);
        return token;
    }
}
